package com.sskj.hangqing.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.sskj.common.base.HttpData;
import com.sskj.hangqing.http.HttpConfig;
import com.sskj.hangqing.ui.fragment.CoinListFragment;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.http.JsonCallBack;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;


public class CoinFragmentPresenter extends BasePresenter<CoinListFragment> {

    /**
     * 行情数据
     *
     * @return
     */
    public void getProduct(UserData userData,String qu, boolean isUpDown, boolean isNew, boolean isSelf) {
        GetRequest<HttpData<List<CoinBean>>> product;
        if (isUpDown) {
//            product = httpService.getProductUpDown();
            product = httpService.getProduct(qu, null);
        } else if (isNew) {
            product = httpService.getProduct(qu, "2");
        } else if (isSelf) {
            if (userData==null){
                mView.updateUI(new ArrayList<>());
                return;
            }
            product = httpService.getProduct(qu, "1");
        } else {
            product = httpService.getProduct(qu);
        }
        product.tag(this).execute(new JsonCallBack<HttpData<List<CoinBean>>>() {
            @Override
            public void onSuccess(Response<HttpData<List<CoinBean>>> response) {
                HttpData<List<CoinBean>> httpData = response.body();
                if (httpData.getStatus() == HttpConfig.OK && httpData.getData() != null) {
                    Disposable disposable = Flowable.fromIterable(httpData.getData())
                            .filter(coinBean -> coinBean.getCode() != null)
                            .toList()
                            .subscribe(coinBeans -> {
                                mView.updateUI(coinBeans);
                            }, e -> {
                            });
                }
            }
        });
    }
}
